package com.yic.module.base.service.erpcentermapping;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.base.controller.admin.erpcentermapping.vo.*;
import com.yic.module.base.dal.dataobject.erpcentermapping.ErpCenterMappingDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.base.convert.erpcentermapping.ErpCenterMappingConvert;
import com.yic.module.base.dal.mysql.erpcentermapping.ErpCenterMappingMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.base.enums.ErrorCodeConstants.*;

/**
 * ERP工作中心映射 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ErpCenterMappingServiceImpl implements ErpCenterMappingService {

    @Resource
    private ErpCenterMappingMapper erpCenterMappingMapper;

    @Override
    public Long createErpCenterMapping(ErpCenterMappingCreateReqVO createReqVO) {
        // 插入
        ErpCenterMappingDO erpCenterMapping = ErpCenterMappingConvert.INSTANCE.convert(createReqVO);
        erpCenterMappingMapper.insert(erpCenterMapping);
        // 返回
        return erpCenterMapping.getId();
    }

    @Override
    public void updateErpCenterMapping(ErpCenterMappingUpdateReqVO updateReqVO) {
        // 校验存在
        validateErpCenterMappingExists(updateReqVO.getId());
        // 更新
        ErpCenterMappingDO updateObj = ErpCenterMappingConvert.INSTANCE.convert(updateReqVO);
        erpCenterMappingMapper.updateById(updateObj);
    }

    @Override
    public void deleteErpCenterMapping(Long id) {
        // 校验存在
        validateErpCenterMappingExists(id);
        // 删除
        erpCenterMappingMapper.deleteById(id);
    }

    private void validateErpCenterMappingExists(Long id) {
        if (erpCenterMappingMapper.selectById(id) == null) {
            throw exception(ERP_CENTER_MAPPING_NOT_EXISTS);
        }
    }

    @Override
    public ErpCenterMappingDO getErpCenterMapping(Long id) {
        return erpCenterMappingMapper.selectById(id);
    }

    @Override
    public List<ErpCenterMappingDO> getErpCenterMappingList(Collection<Long> ids) {
        return erpCenterMappingMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ErpCenterMappingDO> getErpCenterMappingPage(ErpCenterMappingPageReqVO pageReqVO) {
        return erpCenterMappingMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ErpCenterMappingDO> getErpCenterMappingList(ErpCenterMappingExportReqVO exportReqVO) {
        return erpCenterMappingMapper.selectList(exportReqVO);
    }

    @Override
    public ErpCenterMappingDO getErpCenterMapping(String erpCenterId) {
        return erpCenterMappingMapper.selectByErpCenterId(erpCenterId);
    }

}
